tinyMCEPopup.requireLangPack();

var StyleLibraryImageDialog = {
	init : function() {
		var f = document.forms[0];

		document.getElementById('previewbg_pickcontainer').innerHTML = getColorPickerHTML('previewbg_pick','previewbg');

		//fntFont.init(tinyMCEPopup.editor);
		cellProp.init(tinyMCEPopup.editor);
		BorderPicker.init(tinyMCEPopup.editor);
		//mrgMargin.init(tinyMCEPopup.editor);
		
		TinyMCE_EditableSelects.init();

		initDesigner("dialog");
	}
};

tinyMCEPopup.onInit.add(StyleLibraryImageDialog.init, StyleLibraryImageDialog);

function initDesigner(caller)
{
	// Run only if all loaded
	switch(caller) {
		case "frame":
			frameLoaded = true;
			break;
		case "dialog":
			dialogLoaded = true;
			break;
		case "css":
			if(tinymce.isGecko) { return; }  // Never runs. Gecko does not call this way.	
			cssLoaded = true;
			break;
	}
	if(!frameLoaded || !dialogLoaded || (!tinymce.isGecko && !cssLoaded)) {
		return;
	}

	// Configure which rules allow what properties.

	// All has border except caption
	designer.rules[0].properties.initCellProp();
	designer.rules[0].properties.killProperty('text-align');
	designer.rules[0].properties.killProperty('vertical-align');
	designer.rules[0].properties.initBorder();
	
	// Load necessary CSS files	in preview frame
	loadPreviewStyles(window.frames['demoFrame'].document, 
		tinyMCEPopup.editor.getParam("stylelibrary_previewstyles", 
			tinyMCEPopup.editor.getParam("content_css")));

	// Parse input, get dom pointers, create dom rules.
	designer.init(window.frames['demoFrame'].document, "wysiwyg");

	previewSettings.switchLorem(false, true);
	previewSettings.demoBgInit();
	setDemoClass();
	setTask('style');
	updateDemo();
}

function updateDemo()
{
	var output = document.getElementById("hdnCssOutput");
	
	designer.generateOutput();
	designer.updateCssDom();
	output.value = removeNL(designer.getCss());
	
	if(DEBUGJS)
	{
		alert(output.value);
	}
}


// Set the loaded class name on demo table.
function setDemoClass()
{
	var styleName = getStyleName();
	var target = window.frames['demoFrame'].document;

	jQuery("img[id^=demoImage]",target).attr('class',styleName);
}

// ****** EVENTS ********

function taskChanged()
{
	var task = getTask();

	switch(task) {
		case "style":
			BorderPicker.initFromRule(designer.rules[0]);
			$("#fldPickFont").hide();
			cellProp.initFromRule(designer.rules[0]);
			break;
	}
	window.frames['demoFrame'].document.getElementById("demoImage").style.display = 'inline';
}
